<template>
  <div class="my-select">
    <div class="result" @click="openOptions">{{ data[curIdx].text }}</div>
    <div class="options" v-show="optionsShow">
      <div
        class="option"
        v-for="(item, index) of data"
        :key="item.id"
        @click="setOption(index, item)"
      >
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "MySelect",
  data() {
    return {
      curIdx: this.currentIndex,
      optionsShow: false,
    };
  },
  props: {
    data: Array,
    currentIndex: {
      type: Number,
      default: 0,
    },
  },
  methods: {
    openOptions() {
      this.optionsShow = true;
    },
    setOption(index, item) {
      this.curIdx = index;
      //点击完隐藏
      this.optionsShow = false;
      this.$emit("setOption", index, item);
    },
  },
};
</script>

<style lang="scss" scoped>
.my-select {
  position: relative;
  width: 200px;
  height: 34px;
  border: 1px solid #000;
  cursor: pointer;

  .result {
    width: 100%;
    height: 100%;
    line-height: 34px;
    text-align: center;
  }

  .options {
    position: absolute;
    top: 35px;
    left: -1px;
    width: 200px;
    background-color: #fff;

    .option {
      text-align: center;
      height: 34px;
      width: 100%;
      line-height: 34px;
      border: 1px solid #000;
      box-sizing: border-box;
    }
  }
}
</style>